import demo.service.RemoteService;

import javax.naming.Context;
import javax.naming.InitialContext;

/**
 * Created by 张少昆 on 2017/8/7.
 */
public class Client {
    public static void main(String[] args){
        String name = "service-cc";
        String url = "rmi://localhost/";
        try{
            Context namingContext = new InitialContext();
            // 检索指定的对象。 即找到服务器端相对应的服务对象存根
            RemoteService remoteService = (RemoteService) namingContext.lookup(url + name);
            Class stubClass = remoteService.getClass();
            System.out.println(remoteService + " 是 " + stubClass.getName() + " 的实例！");

            // 获得本底存根已实现的接口类型
            Class[] interfaces = stubClass.getInterfaces();
            for(Class c : interfaces){
                System.out.println("存根类实现了 " + c.getName() + " 接口！");
            }
            System.out.println(remoteService.test("你好！"));

            // 通过下面的代码表明：如果已经查找到服务，那就算注册中心挂掉，也不会影响使用！！！
			while (true) {
				Thread.sleep(5000);
				String hello = service.test("哇哈哈1");
				System.out.println(hello);
			}
        } catch(Exception e){
            e.printStackTrace();
        }
    }
}